今天閱讀「適用於 Android 開發人員的 Jetpack Compose」第一章「Compose 基礎知識」的「實作遷移作業」的 1~6 章
dependencies {
// Compose
def composeBom = platform('androidx.compose:compose-bom:2022.10.00')
implementation(composeBom)
androidTestImplementation(composeBom)
implementation "androidx.compose.runtime:runtime"
implementation "androidx.compose.ui:ui"
implementation "androidx.compose.foundation:foundation"
implementation "androidx.compose.foundation:foundation-layout"
implementation "androidx.compose.material:material"
implementation "androidx.compose.runtime:runtime-livedata"
implementation "androidx.compose.ui:ui-tooling"
}
ComposeView
來取代 Layout 區塊@Composable
fun PlantDetailDescription() {
Surface {
Text("Hello Compose")
}
}
class PlantDetailFragment : Fragment() {
override fun onCreateView(...): View? {
val binding = DataBindingUtil.inflate<FragmentPlantDetailBinding>(
inflater, R.layout.fragment_plant_detail, container, false
).apply {
composeView.setContent { //<----這裡
// You're in Compose world!
MaterialTheme {
PlantDetailDescription()
}
}
}
}
}
<TextView
android:text="@{viewModel.plant.name}"
android:textAppearance="?attr/textAppearanceHeadline5"
android:layout_width="match_parent "
android:layout_marginStart="@dimen/margin_small"
android:layout_marginEnd="@dimen/margin_small"
android:gravity="center_horizontal"
/>
Text(
text = name,
style = MaterialTheme.typography.h5,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = dimensionResource(R.dimen.margin_small))
.wrapContentWidth(Alignment.CenterHorizontally)
)